package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 攻略controller
 */

@RestController
@RequestMapping("/strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyDetailService strategyDetailService;
    @Autowired
    private IStrategyCommentService strategyCommentService;


    @Autowired
    private ITravelCommendService travelCommendService;

    /**
     * 分页及条件查询攻略
     * @param qo
     * @return
     */
    @GetMapping
    public PageInfo list(@ModelAttribute("qo") StrategyQueryObject qo){
        return strategyService.query(qo);
    }

    /**
     * 获取某个攻略
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Strategy get(@PathVariable Long id){
        return strategyService.get(id);
    }

    /**
     * 查询某个攻略的分类
     * @param id
     * @return
     */
    @GetMapping("/{id}/strategyCatalog")
    public List<StrategyCatalog> getCatalog(@PathVariable Long id){
        return strategyCatalogService.listByStrategyId(id);
    }


    /**
     *获取攻略文章 及 文章的具体内容
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    public StrategyDetail getDetail(@PathVariable Long id){
        return strategyDetailService.get(id);
    }

    /**
     * 查询某个攻略的评论
     * @param qo : 攻略的id 及 分页
     * @return
     */
    @GetMapping("/{strategyId}/strategyComments")
    public PageInfo getComment(@ModelAttribute("qo") StrategyCommentQueryObject qo){
        //设置每页数量
        qo.setPageSize(4);
        qo.setOrderBy("sc.createTime desc");
        return strategyCommentService.queryCommentByStrategyId(qo);
    }

    /**
     * 保存某个攻略的评论
     * @param strategyComment : 评论对象
     * @param imgs : 图片数组
     * @return
     */
    @PostMapping("/{strategy.id}/strategyComment")
    public JsonResult saveComment(StrategyComment strategyComment, String[] imgs){
        JsonResult jsonResult=new JsonResult();
        try {
            strategyCommentService.save(strategyComment,imgs);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("操作异常");
        }
        return jsonResult;
    }


    /**
     * 攻略的常用标签
     * @param qo
     * @return
     */
    @GetMapping("/{id}/tags")
    public PageInfo getTags(StrategyCommentQueryObject qo){
        //设置常用前六个标签
        qo.setPageSize(6);
        qo.setCurrentPage(1);
        return strategyCommentService.getTags(qo);
    }

    /**
     * 查询游记
     */
    @GetMapping("/{id}/travelcommend")
    public List<TravelCommend> listAll(@PathVariable Long id){
        return travelCommendService.selectAll(id);
    }
}
